package com.hh.generator.core.generator;

import com.hh.generator.common.dto.ColumnEntity;
import com.hh.generator.common.dto.TableEntity;
import com.hh.generator.core.Generator;
import com.hh.generator.core.model.ClassModel;
import com.hh.generator.core.model.Config;
import com.hh.generator.core.model.ConverterModel;

import java.util.ArrayList;
import java.util.List;

import static com.hh.generator.common.util.TableToClassConvertUtil.*;

public class ConverterGenerator extends AbstractTableBaseGenerator implements Generator {

    private String templateName = "Converter.java.vm";

    public ConverterGenerator(Config config, TableEntity tableEntity, List<ColumnEntity> columnEntityList) {
        super(config, tableEntity, columnEntityList);
    }

    @Override
    public String fileName() {
        return converterClassName(config.getTablePrefix(), tableEntity.getTableName()) + ".java";
    }

    @Override
    public ClassModel buildModel() {
        List<String> imports = new ArrayList<>();
        String infoClassName = infoClassName(config.getTablePrefix(), tableEntity.getTableName());
        String infoClassFullName = config.getInfoPackage().replace("xx", config.getModule()) + "." + infoClassName;
        String entityClassName = className(config.getTablePrefix(), tableEntity.getTableName());
        String entityClassFullName = config.getEntityPackage().replace("xx", config.getModule()) + "." + entityClassName;
        imports.add(config.getConverterClassName());
        imports.add(infoClassFullName);
        imports.add(entityClassFullName);
        ConverterModel model = new ConverterModel();
        model.setInfoClassName(infoClassName);
        model.setEntityClassName(entityClassName);
        model.setPackageName(config.getConverterPackage().replace("xx", config.getModule()));
        model.setImports(imports);
        model.setComment(tableEntity.getTableComment() + " Converter转换器");
        model.setAuthor(config.getClassAuthor());
        model.setDate(config.getDate());
        model.setClassName(converterClassName(config.getTablePrefix(), tableEntity.getTableName()));
        return model;
    }

    @Override
    public String getTemplateName() {
        return templateName;
    }

}
